package com.ziki.framework.wicket.behavior;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.util.lang.Args;

import com.ziki.common.util.string.StrUtil;

public class FwAttributeModifier extends AttributeAppender {
    private static final long serialVersionUID = 1L;

    public FwAttributeModifier(String attribute, String value) {
        super(attribute, value);
    }

    public FwAttributeModifier(String attribute, String value, String separator) {
        super(attribute, value);
        setSeparator(separator);
    }

    public String getValue() {
        return StrUtil.parseStr(getReplaceModel());
    }

    public static FwAttributeModifier appendValue(String attributeName, String value) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, value, " ");
    }

    public static FwAttributeModifier prependValue(String attributeName, String value) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, value, " ") {
            private static final long serialVersionUID = 1L;

            protected String newValue(String currentValue, String replacementValue) {
                return super.newValue(replacementValue, currentValue);
            }
        };
    }

    public static FwAttributeModifier replaceValue(String attributeName, String value) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, value) {
            private static final long serialVersionUID = 1L;

            protected String newValue(String currentValue, String replacementValue) {
                return replacementValue;
            }
        };
    }

    public static FwAttributeModifier removeValue(String attributeName, String value) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, value) {
            private static final long serialVersionUID = 1L;

            protected String newValue(String currentValue, String replacementValue) {
                return StrUtil.isEmpty(currentValue) ? "" : currentValue.replaceAll(replacementValue, "");
            }
        };
    }

    public static FwAttributeModifier removeAttr(String attributeName) {
        Args.notEmpty(attributeName, "attributeName");
        return new FwAttributeModifier(attributeName, VALUELESS_ATTRIBUTE_REMOVE);
    }

}
